<template>
  <el-popover placement="right"
              width="50%"
              @show="search"
              trigger="hover">
    <el-table :data="tableData"
              v-loading="loading"
              show-summary
              border
              stripe>
      <el-table-column prop="contractId"
                       label="合同号"
                       sortable
                       align="center"
                       width="100" />
      <el-table-column prop="contractName"
                       label="项目名称"
                       sortable
                       align="center"
                       min-width="120">
        <template slot-scope="props">
          <el-popover placement="bottom"
                      trigger="hover"
                      :content="props.row.contractName">
            <a slot="reference"
               class="table-overflow-hidden">{{props.row.contractName}}</a>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="territory"
                       label="责任区域"
                       sortable
                       align="center"
                       width="85" />
      <el-table-column prop="primaryTerritory"
                       label="主跟进组"
                       sortable
                       align="center"
                       width="85" />
      <el-table-column prop="signType"
                       label="签订类型"
                       sortable
                       align="center"
                       width="85" />
      <el-table-column prop="signAmount"
                       label="签订金额"
                       sortable
                       align="center"
                       width="85" />
      <el-table-column prop="contractingOfficer"
                       label="签订人员"
                       sortable
                       align="center"
                       width="85" />
    </el-table>
    <!-- 分页 -->
    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :total="pagging.total"
                   :page-sizes="pagging.sizes"
                   :current-page="pagging.currentPage"
                   @size-change="sizeChange"
                   @current-change="pageChange">
    </el-pagination>
    <slot slot="reference"></slot>
  </el-popover>
</template>
<script>
import Moment from 'moment'
import { getMLists } from '@/api/maintenance'
export default {
  props: {
    territory: {
      type: String
    },
    primaryTerritory: {
      type: String
    },
    signType: {
      type: String
    },
    planMonth: {
      type: String
    },
    planYear: {
      type: [Date, String]
    }
  },
  data() {
    return {
      loading: false,
      tableData: [],
      // 分页属性
      pagging: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        sizes: [10, 30, 50, 100]
      }
    }
  },
  methods: {
    search() {
      this.pagging.currentPage = 1
      this.getTableData()
    },
    // 每页显示条数变更新
    sizeChange(size) {
      this.pagging.pageSize = size
      this.getTableData()
    },
    // 跳转到页面
    pageChange(curPage) {
      this.pagging.currentPage = curPage
      this.getTableData()
    },
    // 请求服务合同列表数据
    getTableData() {
      this.loading = true
      const params = {
        signType: this.signType,
        territory: this.territory,
        contractDate: '',
        projectMaturity: '已签',
        primaryTerritory: this.primaryTerritory,
        pageIndex: this.pagging.currentPage,
        pageSize: this.pagging.pageSize
      }

      if (this.planYear !== '' && this.planYear !== null) {
        params.contractDate = Moment(this.planYear).format('YYYY')
      } else if (this.planMonth !== '' && this.planMonth !== null) {
        params.contractDate = Moment(this.planMonth).format('YYYY-MM')
      } else {
        params.contractDate = Moment().format('YYYY-MM')
      }

      // 请求数据
      getMLists(params).then(res => {
        if (res.data.success) {
          const result = res.data.data
          this.tableData = result.data
          this.pagging.total = result.totalCount
          this.pagging.currentPage = result.pageIndex
          this.pagging.pageSize = result.pageSize
          this.loading = false
        }
      })
    }
  }
}
</script>
